#pragma once

#include "fxcc/core/graph/opengl3/TextureCubeJack.h"
#include "fxcc/core/graph/opengl3/RenderBuffer.h"
#include "fxcc/core/graph/opengl3/FrameBuffer.h"
#include "fxcc/core/graph/opengl3/Texture2DJack.h"

struct FXCC_API  RenderTargetCube :public fxcc::Base
{
    struct FXCC_API  Desc
    {
        fxcc::graph::common::ImageMipmap::MipMapData m_FaceMipmapData;
    } m_Desc;

    std::shared_ptr<fxcc::graph::opengl3::TextureCubeJack> m_TextureCube;
    std::shared_ptr<fxcc::graph::opengl3::FrameBuffer> m_FrameBuffer;
    std::shared_ptr<fxcc::graph::opengl3::RenderBuffer> m_RenderBuffer;

    RenderTargetCube(const Desc& desc) :m_Desc(desc)
    {
        m_Available = Init();
    }
    bool Init()
    {
        fxcc::graph::common::TextureCubeJack::Desc cubeDesc;
        cubeDesc.m_Type = fxcc::graph::common::TextureCubeJack::Desc::Type::_ALLOC_;
        cubeDesc.m_HDRConfig = m_Desc.m_FaceMipmapData;
        m_TextureCube = std::make_shared<fxcc::graph::opengl3::TextureCubeJack>(cubeDesc);

        m_FrameBuffer = std::make_shared<fxcc::graph::opengl3::FrameBuffer>();
        m_RenderBuffer = std::make_shared<fxcc::graph::opengl3::RenderBuffer>();

        m_FrameBuffer->Bind();
        m_RenderBuffer->Bind();
        m_RenderBuffer->Storage(GL_DEPTH_COMPONENT24, m_Desc.m_FaceMipmapData.m_Width, m_Desc.m_FaceMipmapData.m_Height);
        m_FrameBuffer->FrameRenderBuffer(GL_DEPTH_ATTACHMENT, m_RenderBuffer->m_Id);


        fxcc::graph::opengl3::FrameBuffer::UnBind();
        fxcc::graph::opengl3::RenderBuffer::UnBind();
        fxcc::graph::opengl3::Texture2DJack::UnBind();
        fxcc::graph::opengl3::TextureCubeJack::UnBind();
        return true;
    }
    void Begin(int face)
    {
        glViewport(0, 0, m_Desc.m_FaceMipmapData.m_Width, m_Desc.m_FaceMipmapData.m_Width);
        m_FrameBuffer->Bind();
        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, m_TextureCube->m_Id, 0);

    }
    static void End()
    {
        fxcc::graph::opengl3::FrameBuffer::UnBind();
        fxcc::graph::opengl3::RenderBuffer::UnBind();
        fxcc::graph::opengl3::Texture2DJack::UnBind();
        fxcc::graph::opengl3::TextureCubeJack::UnBind();
    }
};